import Header from '../components/layout/Header'
import Hero from '../components/home/Hero'
import React from 'react'
import Post from '../components/home/Post'
import './home.scss'

class Home extends React.Component{
  constructor(props) {
    super(props)
    this.state = {
      count: 0
    }
  }

  // 例1-3 存在一个长度为 n 的字符串，串中元素互不相同。编写一算法，确定字符 e 在字符串中的位置
  // 长度 n 用 length获取
  search(text) {
    let arr = text.split('')
    let length = arr.length
    let i
    for (i = 1; i <= length; i++) {
      if (arr[i] === 'e') {
        return i
      }
      // i 等于 n时仍未找到字符 e , 则字符串中没有 e 这个字符，i 的值为0
      if (i === length){
        return 0
      }
    }
  }

  // 例1-4 求 4 x 4 矩阵元素和
  // arrNum: int[4][4]
  // 问题规模 n=4; 频度为 f(4)=4*4; 时间复杂度为 T(4)=O(f(4))
  sum(arrNum) {
    let r = 0
    for (let i = 0; i < 4; i++) {
      for (let j = 0; j < 4; j++) {
        r += arrNum[i][j]
      }
    }
    return r
  }

  // 对3个两位数按由大到小的顺序进行排序
  sortThreeNum(arr) {
    for (let i = 0; i < arr.length; i++) {
      for (let j = i; j < arr.length; j++) {
        if (arr[j] > arr[i]) {
          let temp = arr[i]
          arr[i] = arr[j]
          arr[j] = temp
        }
      }
    }
    return arr
  }

  componentDidMount() {
    // this.setState({count: this.state.count + 1 })

    let text = 'abcdpppfg'
    let i = this.search(text)
    console.log(i)

    let arr2D = [
      [1,2,3,4],
      [1,2,3,8],
      [1,2,3,4],
      [1,2,3,4],
    ]
    let r = this.sum(arr2D)
    console.log(r)

    let arrNum = [1,2,3,4,5,6,7,8,9,10]
    let res = this.sortThreeNum(arrNum)
    console.log(res)
  }

  render() {
    return (
      <section className='home-container'>
        <Header></Header>
        <Hero></Hero>
        <section className='main-container'>
          <main>
            {
              [1,2,3,4].map(x => {
                return (
                  <Post key={x}></Post>
                )
              })
            }
          </main>
          <aside>

          </aside>
        </section>
      </section>
    )
  }
}

export default Home
